<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class SettlementWeightExport implements FromQuery, WithHeadings, WithMapping
{
    use ModelOperatorTrait;

	protected $conditions;

	public function __construct($conditions = []){
		$this->conditions = $conditions;
	}

	protected function model(){
		return app(\App\Models\SettlementWeight::class);
	}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
    	$settlementWeight = $this->model();

    	if(isset($this->conditions['system_number'])){
    		$settlementWeight = $settlementWeight->where('system_number','=',$this->conditions['system_number']);
    	}

    	if(isset($this->conditions['start_time'])){
    		$settlementWeight = $settlementWeight->where('updae_datetime','>=',$this->conditions['start_time']);
    		$settlementWeight = $settlementWeight->where('updae_datetime','<=',$this->conditions['end_time']);
    	}

    	$settlementWeight = $settlementWeight->orderBy('updae_datetime','DESC');

        return $settlementWeight;
    }

    public function map($settlementWeight):array{
    	return [
    		$settlementWeight->system_number,
    		$settlementWeight->vehicle_number,
    		$settlementWeight->driver_name,
    		'\''.$settlementWeight->driver_idcard,
    		$settlementWeight->driver_mobile_phone,
    		$settlementWeight->actual_weight,
    		$settlementWeight->system_datetime,
    		$settlementWeight->is_weight_desc,
    		$settlementWeight->updae_datetime,
    		$settlementWeight->operator
    	];
    }

    public function headings():array{
    	return [
    		'单据流水号',
    		'车牌号',
    		'司机姓名',
    		'身份证号码',
    		'手机号码',
    		'实际（吊秤）重量（kg）',
    		'装货日期',
    		'是否过磅',
    		'更新时间',
    		'操作人员'
    	];
    }
}
